/**
 * 
 */
package server.alarmThreads;


import java.io.IOException;


import server.config.ServerConfig;
import server.container.EinsatzAlarm;


/**
 * @author Christian
 * 
 */
public class LocalAlarmThread implements AlarmThread {
	
	
	@Override
	public void alarmiere(EinsatzAlarm alarm) {

		if (alarm.getSchleife().equals("06-52")) {
			new Thread(new Runnable() {

				@Override
				public void run() {

					LocalAlarmThread player = new LocalAlarmThread();
					player.setMuteState(false);
					player.setVolume(65535);
					player.play("sound/feuer.mp3");
					try {
						Thread.sleep(30 * 1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					player.setVolume(26000);
				}
			}).start();
		}
	}
	
	
	
	/**
	 * @param string
	 */
	private void play(String filePfad) {

		StringBuilder exceBuilder = new StringBuilder();
		exceBuilder.append(ServerConfig.getInstance().getVlcPfad());
		exceBuilder.append(" --qt-autosave-volume ");
		exceBuilder.append(filePfad);
		Runtime runtime = Runtime.getRuntime();
		try {
			runtime.exec(exceBuilder.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @param wert
	 *            0-65535
	 */
	private void setVolume(int wert) {

		StringBuilder exceBuilder = new StringBuilder();
		exceBuilder.append(ServerConfig.getInstance().getNircmdPfad());
		exceBuilder.append(" ");
		exceBuilder.append("setsysvolume");
		exceBuilder.append(" ");
		exceBuilder.append(wert);
		Runtime runtime = Runtime.getRuntime();
		try {
			runtime.exec(exceBuilder.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(exceBuilder.toString());
	}
	
	private void setMuteState(boolean mute) {

		StringBuilder exceBuilder = new StringBuilder();
		exceBuilder.append(ServerConfig.getInstance().getNircmdPfad());
		exceBuilder.append(" ");
		exceBuilder.append("mutesysvolume");
		exceBuilder.append(" ");
		if (mute) {
			exceBuilder.append(1);
		} else {
			exceBuilder.append(0);
		}
		Runtime runtime = Runtime.getRuntime();
		try {
			runtime.exec(exceBuilder.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {

		try {
			new ServerConfig("serverConfigLocal.cfg");
		} catch (IOException e) {
			e.printStackTrace();
		}
		LocalAlarmThread player = new LocalAlarmThread();
		player.setMuteState(false);
		player.setVolume(65535);
		player.play("sound/feuer.mp3");
		try {
			Thread.sleep(10 * 1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		player.play("sound/feuer.mp3");
		try {
			Thread.sleep(10 * 1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		player.setVolume(26000);
	}



	
}
